
<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="优惠券名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入优惠券名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['CouponTemplate:template:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['CouponTemplate:template:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['CouponTemplate:template:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['CouponTemplate:template:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" width="80" />
      <el-table-column label="优惠券名称" align="center" prop="name" min-width="150" />
      <el-table-column label="类型" align="center" prop="type" width="120">
        <template #default="scope">
          {{ scope.row.type === 1 ? '满减' : scope.row.type === 2 ? '折扣' : '无门槛' }}
        </template>
      </el-table-column>
      <el-table-column label="面值" align="center" prop="faceValue" width="80" />
      <el-table-column label="最低消费金额" align="center" prop="minOrderAmount" width="120" />
      <el-table-column label="适用范围" align="center" prop="applicableScope" width="180">
        <template #default="scope">
          {{ scope.row.applicableScope === 0 ? '全部' : scope.row.applicableScope === 1 ? '指定商品' : '指定品类' }}
        </template>
      </el-table-column>
      <el-table-column label="发放总量" align="center" prop="totalQuantity" width="100" />
      <el-table-column label="每人限领数量" align="center" prop="userLimit" width="120" />
      <el-table-column label="有效期类型" align="center" prop="validityType" width="120">
        <template #default="scope">
          {{ scope.row.validityType === 1 ? '固定日期' : '领取后生效' }}
        </template>
      </el-table-column>
      <el-table-column label="有效期开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效天数" align="center" prop="validDays" width="100" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          {{ scope.row.status === 1 ? '生效' : '失效' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['CouponTemplate:template:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['CouponTemplate:template:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠卷对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="templateRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="优惠券名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入优惠券名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="满减" value="1" />
            <el-option label="折扣" value="2" />
            <el-option label="无门槛" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="面值" prop="faceValue">
          <el-input v-model="form.faceValue" placeholder="请输入面值" />
        </el-form-item>
        <el-form-item label="最低消费金额" prop="minOrderAmount">
          <el-input v-model="form.minOrderAmount" placeholder="请输入最低消费金额" />
        </el-form-item>
        <el-form-item label="适用范围" prop="applicableScope">
          <el-select v-model="form.applicableScope" placeholder="请选择适用范围">
            <el-option label="全部" value="0" />
            <el-option label="指定商品" value="1" />
            <el-option label="指定品类" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="发放总量" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入发放总量" />
        </el-form-item>
        <el-form-item label="每人限领数量" prop="userLimit">
          <el-input v-model="form.userLimit" placeholder="请输入每人限领数量" />
        </el-form-item>
        <el-form-item label="有效期类型" prop="validityType">
          <el-select v-model="form.validityType" placeholder="请选择有效期类型">
            <el-option label="固定日期" value="1" />
            <el-option label="领取后生效" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="有效期开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择有效期开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择有效期结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效天数" prop="validDays">
          <el-input v-model="form.validDays" placeholder="请输入领取后有效天数" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="生效" value="1" />
            <el-option label="失效" value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Template">
import { listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate } from "@/api/CouponTemplate/template"

const { proxy } = getCurrentInstance()

const templateList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null,
    status: null
  },
  rules: {
    name: [
      { required: true, message: "优惠券名称不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    faceValue: [
      { required: true, message: "面值不能为空", trigger: "blur" }
    ],
    totalQuantity: [
      { required: true, message: "发放总量不能为空", trigger: "blur" }
    ],
    validityType: [
      { required: true, message: "有效期类型不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询优惠卷列表 */
function getList() {
  loading.value = true
  listTemplate(queryParams.value).then(response => {
    templateList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    type: null,
    faceValue: null,
    minOrderAmount: null,
    applicableScope: null,
    totalQuantity: null,
    userLimit: null,
    validityType: null,
    startTime: null,
    endTime: null,
    validDays: null,
    status: null
  }
  proxy.resetForm("templateRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加优惠卷"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getTemplate(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改优惠卷"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["templateRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTemplate(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addTemplate(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除优惠卷编号为"' + _ids + '"的数据项？').then(function() {
    return delTemplate(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('CouponTemplate/template/export', {
    ...queryParams.value
  }, `template_${new Date().getTime()}.xlsx`)
}

getList()
</script>